package com.xzh.rxjava;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.EditText;

import com.jakewharton.rxbinding2.widget.RxTextView;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;
import io.reactivex.functions.Predicate;
import io.reactivex.schedulers.Schedulers;

/**
 * 作者 ： xiezhihui on 2018\8\26 0026.
 * Q Q ： 347649749
 * 微信 ： LOVE347649749
 * 作用 ：xxx
 */

public class SearchActivity extends AppCompatActivity {

    private EditText mEditText;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search_activity_layout);
        mEditText = findViewById(R.id.mEditText);
        setTextChange();
        /*mEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Log.e("onTextChanged  :  ",s.toString());
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });*/
    }


    private void setTextChange(){
        RxTextView.textChanges(mEditText)
                .debounce(200, TimeUnit.MILLISECONDS)
                .subscribeOn(AndroidSchedulers.mainThread())
                .filter(new Predicate<CharSequence>() {
                    @Override
                    public boolean test(CharSequence charSequence) throws Exception {
                        Log.e("test :  ",charSequence+"    "+charSequence.toString().trim().length());
                        return charSequence.toString().trim().length() > 0;
                    }
                })
                /*.flatMap(new Function<CharSequence, ObservableSource<List<String>>>() {
                    @Override
                    public ObservableSource<List<String>> apply(CharSequence charSequence) throws Exception {
                        List<String> list = new ArrayList<>();
                        list.add("星期一");
                        list.add("星期二");
                        list.add("星期三");
                        return Observable.just(list);
                    }
                })*/
                .switchMap(new Function<CharSequence, ObservableSource<List<String>>>() {
                    @Override
                    public ObservableSource<List<String>> apply(CharSequence charSequence) throws Exception {
                        List<String> list = new ArrayList<>();
                        list.add("星期一");
                        list.add("星期二");
                        list.add("星期三");
                        return Observable.just(list);
                    }
                })
//                .flatMap(new Function<CharSequence, ObservableSource<List<String>>>() {
//                    @Override
//                    public ObservableSource<List<String>> apply(CharSequence charSequence) throws Exception {
//                        List<String> list = new ArrayList<>();
//                        list.add("星期一");
//                        list.add("星期二");
//                        list.add("星期三");
//                        return Observable.just(list);
//                    }
//                })

                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<String>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(List<String> strings) {
                        Log.e("onNext :  ",strings.toString());
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
